# -*- coding: utf-8 -*-
from selenium.webdriver.common.keys import Keys
from re import compile, search
import time
import data

class Page(data.Basic_data):
  def __init__(self, driver):
    data.Basic_data.__init__(self)
    self.driver = driver
    self.match_by_obj = compile(r'^[^=]+')
    self.match_what_obj = compile (r'[^=]+$')

  def _get_element_(self, name):
    by_n_what = self.locators[name]
    print "BY N WHAT = ", by_n_what
    by_obj = self.match_by_obj.search(by_n_what)
    what_obj = self.match_what_obj.search(by_n_what)
    print "HOW MATCH = ", by_obj.group(0), "WHAT MATCH = ", what_obj.group(0)
    if by_obj.group(0) == "by_name":
      element_obj = self.driver.find_element_by_name(what_obj.group(0))
      return element_obj
    else:
      #-1 = unknown way of searching
      return -1


  #def click (self, element_obj):

  #def get_text (self, element_obj):

  def type (self, target, text):
    target.send_keys("admin")

  def clean (self, element_obj):
    element_obj.send_keys(Keys.CONTROL, "a",Keys.DELETE)

  def click_enter (self, element_obj):
    element_obj.send_keys(Keys.RETURN)

  def take_screenshot(self, name_of_file):
    self.driver.get_screenshot_as_file(name_of_file)

  def wait (self, tsec):
    time.sleep(tsec)


class Login_page (Page):

  def __init__(self, driver):
    Page.__init__(self, driver)
    self.locators= {"username" : "by_name=username", "password" : "by_name=password"}
    self.driver = driver
    self.driver.get(self.driver.base_url + "/php4dvd")

  def login(self, username, password):
    username_field_obj = self._get_element_("username")
    self.clean(username_field_obj)
    self.type(username_field_obj, username)
    password_field_obj = self._get_element_("password")
    self.clean(password_field_obj)
    self.type(password_field_obj, password)
    self.click_enter(password_field_obj)
    self.wait(10)
    self.take_screenshot("login")
